/*
 * @Author: szx
 * @Date: 2022-03-16 13:45:25
 * @LastEditTime: 2022-03-16 13:47:37
 * @Description:
 * @FilePath: \leetcode\1-99\53\53_2.js
 */
/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function (nums) {
    let res = -Infinity;
    let sum = 0;
    for (let i of nums) {
        sum += i;
        res = Math.max(res, sum);
        if (sum < 0) {
            sum = 0;
        }
    }
    return res;
};
console.log(maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4]));
console.log(maxSubArray([1]));
console.log(maxSubArray([5, 4, -1, 7, 8]));
console.log(maxSubArray([-4, -2, -3]));
